package samber

import (
	"fmt"
	"github.com/samber/lo"
)

// Difference 排除两者之间的并集
func Difference() {
	list1 := []int{1, 2, 3, 4}
	list2 := []int{2, 3, 4, 5, 6}
	noIn2, noIn1 := lo.Difference(list1, list2)
	fmt.Printf("%v %v\n", noIn2, noIn1) // [1] [5 6]
}

// DispatchingStrategyFirst 如果超过第一个通道的容量，将选择第二个通道，依此类推。
// 我暂时没懂这个源码 QAQ 可能类似一个监听？ 从第一个开始判断直到有一个有位置可以使用？
func DispatchingStrategyFirst() {
	channel1 := make(chan string, 2)
	channel2 := make(chan string, 2)
	channel3 := make(chan string, 2)
	channels := []<-chan string{channel1, channel2, channel3}
	res := lo.DispatchingStrategyFirst("Hello", 1, channels)
	fmt.Printf("%v \n", res)
}

// DispatchingStrategyLeast 选出最小长度
func DispatchingStrategyLeast() {
	channel1 := make(chan string, 1)
	channel1 <- "1"
	channel2 := make(chan string, 2)
	channel2 <- "1"
	channel3 := make(chan string, 3)
	channels := []<-chan string{channel1, channel2, channel3}

	res := lo.DispatchingStrategyLeast("Hello", 0, channels)
	fmt.Printf("%v \n", res) // 2
}

func DispatchingStrategyMost() {
	channel1 := make(chan string, 3)
	channel2 := make(chan string, 2)
	channel3 := make(chan string, 1)
	channel1 <- "1"
	channel1 <- "2"
	channel1 <- "3"
	channel2 <- "2"
	channel3 <- "3"
	channels := []<-chan string{channel1, channel2, channel3}
	res := lo.DispatchingStrategyMost("Hello", 0, channels)
	// 0 返回的是0 要稍稍理解下为啥 多了个判断是不是满了 导致 3者2比较之间都是false => 返回一个 0
	// 按照我的理解这个函数应该是返回 2 因为就2能塞下了 打了一会脑子
	fmt.Printf("%v \n", res) // 0
}

// DispatchingStrategyRandom 超过信道容量，将选择另一个随机信道
func DispatchingStrategyRandom() {
	channel1 := make(chan string, 3)
	channel2 := make(chan string, 2)
	channel3 := make(chan string, 1)
	channel1 <- "1"
	channel1 <- "2"
	channel1 <- "3"
	channel2 <- "2"
	channel3 <- "3"
	channels := []<-chan string{channel1, channel2, channel3}
	res := lo.DispatchingStrategyRandom("Hello", 0, channels)
	fmt.Printf("%v \n", res) // 1 只能为1了因为 其他都塞满了
}

// DispatchingStrategyRoundRobin 跟上面的话这个是轮询方式不是随机
func DispatchingStrategyRoundRobin() {
	channel1 := make(chan string, 3)
	channel2 := make(chan string, 2)
	channel3 := make(chan string, 1)
	channels := []<-chan string{channel1, channel2, channel3}
	res := lo.DispatchingStrategyRoundRobin("Hello", 0, channels)
	fmt.Printf("%v \n", res) // 0
}

// Drop 从左边丢掉2个
func Drop() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.Drop(list, 2)
	fmt.Printf("%v \n", result) // [2 3 4 5]
}

// DropRight 从右边丢2个
func DropRight() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.DropRight(list, 2)
	fmt.Printf("%v \n", result) // [0 1 2 3]
}

func DropRightWhile() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.DropRightWhile(list, func(val int) bool {
		return val > 2
	})
	fmt.Printf("%v \n", result) // [0 1 2]
}

func DropWhile() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.DropWhile(list, func(val int) bool {
		return val < 2
	})
	fmt.Printf("%v \n", result) // [2 3 4 5]
}
